/**
* @desc Core class of the Codereck UI built off of the jQuery javascript library.  Part of the Codereck Javascript Suite.
*
* @copyright Ryan Zec 2007-2009, All Right Reserved
*
* @license BSD (see LICENSE file included, the generic templete can be found at http://www.opensource.org/licenses/bsd-license.php)
*/
;(function($)
{
	//create the codereck scope
	$.cr = $.cr || {};
	$.cr.fn = $.cr.fn || {};

	$.cr.core = function(){};

	//there are some common functions that have been proven useful for me

	/**
	* @desc Converts all form inputs into a url query string.
	* Browsers Tested:
	*
	* @param string Form id
	* @return string Url query
	*/
	$.cr.fn.form_to_string = function(form_id)
	{
        var form_string = '';

        element = $('#' + form_id);
		$(':input', element).each(function()
		{
			if(form_string != '')
			{
				form_string += '&';
			}
			form_string += $(this).attr('name') + '=' + $(this).val();
		});

		return form_string;
	};

    /**
	* @desc Converts all form inputs into a JSON data object.
	* Browsers Tested:
	*
	* @param string Form id
	* @return JSON Data object
	*/
	$.cr.fn.form_to_data_object = function(form_id)
	{
        var form_data = {};

        element = $('#' + form_id);
		$(':input', element).each(function()
		{
			form_data[$(this).attr('name')] = $(this).val();
		});

		return form_data;
	};

    /**
	* @desc Convert a url query string to a JSON data object.
	* Browsers Tested:
	*
	* @param string Url query
	* @return JSON Data object
	*/
	$.cr.fn.url_query_to_data = function(url_query)
	{
        var data = {};

		if(url_query != undefined && url_query != null)
		{
			if(url_query.indexOf('&') !== false)
			{
				var varables = url_query.split('&');
			}
			else
			{
				var varables = new Array(string);
			}

			var length = varables.length;

			for(var x = 0; x < length; x++)
			{
				var key = varables[x].split('=')[0];
				var value = varables[x].split('=')[1];
				data[key] = value;
			}
		}

		return data;
	};

    /**
	* @desc Allows you to preload images.
	* Browsers Tested:
	*
	* @param string Image source
	* @param string optional Image source ...
	* @return void
	*/
	$.cr.fn.preload_images = function()
	{
		for(var i = 0; i<arguments.length; i++)
		{
			$("<img>").attr("src", arguments[i]);
		}
	};

	$.extend($.cr.core,
	{
    	/**
		* @desc Generate a unique id with the passed prefix(it will append a number to the passed prefix)
		* Browsers Tested:
		*
		* @param string Id prefix
		* @return string Unique id
		*/
		generate_id: function(id_prefix)
		{
			return id_prefix + ($('[id^=' + id_prefix + ']').length + 1);
		},

		/**
		* @desc Blocks the UI anything that is under a z-index of 1000.
		* Browsers Tested:
		*
		* @param void
		* @return void
		*/
		block_ui: function()
		{
			if($('#cr_block_ui').length == 0)
			{
	            $('body').append('<div id="cr_block_ui"></div>');
		        var document_height = parseInt($(document).height());
		        var document_width = parseInt($(document).width());

		        $('#cr_block_ui').css(
		        {
	        		'height': document_height,
	        		'width': document_width
				});
			}
		},

        /**
		* @desc Frees the UI from blocking.
		* Browsers Tested:
		*
		* @param void
		* @return void
		*/
		free_ui: function()
		{
			 $('#cr_block_ui').remove();
		}
	});
})(jQuery);